package hos.http.converter;

import java.io.IOException;
import java.lang.reflect.Type;

import hos.http.HttpRequest;

/**
 * <p>Title: HttpConvertString </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/4/4 18:06
 */
public abstract class HttpStringConvert implements HttpConvert<String> {

    public HttpStringConvert() {
    }

    /** @noinspection unchecked*/
    @Override
    public <D> D onConvert(HttpRequest<?> request, Type succeed, String response) throws IOException {
        try {
            return (D) HttpConverterUtils.<D>requireData(request, succeed, response);
        } catch (IOException e) {
            // 解析异常,重新解析
            return parseJson(succeed, response);
        }
    }

    public abstract <D> D parseJson(Type succeed, String json) throws IOException;
}
